"""
author:Yang Yang
"""
import pygame, sys
from pygame.locals import *
import copy
import math

#color
button       = (  0,   150,  200)
brightbutton = (  0,   200,  230)
checkboard   = (213, 176, 146)
line         = (  0,   0,   0)
BROWN        = (128,  42,  42)
WHITE        = (255, 255, 255)
BLACK        = (  0,   0,   0)
AWHITE       = (213, 176, 146,50)
 
#size
length = 37
width_between_line  = 25
height_between_line = 18
width  = 36 * width_between_line
height = 36 * height_between_line
buttonwidth   = 300
buttonheight  = 100
buttonwidth2  = 100
buttonheight2 = 50
boardwidth    = 1280
boardheight   = 680

#directions(top,bottom,left,right,top-left,botton-left,top-right,bottom-right)
direction = [[-1,0], [1,0], [0,-1], [0,1], [-1,-1], [1,-1], [-1,1], [1,1]]


#judge if the game starts
started = False

#judge if player has chosen who to play first
choice = False

iscover = 0
#judge who to play
t = True
#first = True
count = 1
#stop game if someone win
running = True
firstmove = True
end = False

searchset = set()

#chess
blackchess = pygame.image.load('D:\\BetaSix(AI_to_Human)\\black.png')
whitechess = pygame.image.load('D:\\BetaSix(AI_to_Human)\\white.png')


m=[]
for i in  range(length+7):
    m.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
def clear():
    global m
    for i in range(length+7):
        for j in range(length+7):
            m[i][j] = 0

def reset():
    global t, count, firstmove, end
    t = True
    #first = True
    count = 1
    #stop game if someone win
    running = True
    firstmove = True
    end = False

def DrawChessBoard(screen):
    pygame.draw.rect(screen, checkboard,
                     ((boardwidth  - width)/2,(boardheight - height)/2,width,height))
    x1 = (boardwidth  - width)/2
    y1 = (boardheight - height)/2
    x2 = (boardwidth  - width)/2
    y2 = (boardheight - height)/2
    
    for i in range(length):
        pygame.draw.line(screen, line, (x1,y1), (x1+width, y1), 2)
        y1 += height_between_line

    for i in range(length):
        pygame.draw.line(screen, line, (x2,y2), (x2, y2+height), 2)
        x2 += width_between_line

def DrawFirstButton(screen):
    pygame.draw.rect(screen, button, (500, 100, buttonwidth, buttonheight))
    pygame.draw.rect(screen, button, (500, 300, buttonwidth, buttonheight))
    pygame.draw.rect(screen, button, (500, 500, buttonwidth, buttonheight))

    s_font=pygame.font.SysFont('SimHei',30)
    text1  = s_font.render("人机对战",True,WHITE)
    text2  = s_font.render("人人对战",True,WHITE)
    text3  = s_font.render("退出游戏",True,WHITE)
    t_font = pygame.font.SysFont('SimHei',20)
    text4  = t_font.render("说明：选择人机对战，你将挑战BetaSix;选择人人对战，你可以与好友进行对弈", True,BLACK )

    screen.blit(text1,(600,135))
    screen.blit(text2,(600,335))
    screen.blit(text3,(600,535))
    screen.blit(text4,(300,630))
def DrawSecondButton(screen,):
    pygame.draw.rect(screen, button, (500, 100, buttonwidth, buttonheight))
    pygame.draw.rect(screen, button, (500, 400, buttonwidth, buttonheight))

    s_font=pygame.font.SysFont('SimHei',30)
    
    text1=s_font.render("人类先手",True,WHITE)
    text2=s_font.render("BetaSix先手",True,WHITE)
    
    screen.blit(text1,(600,135))
    screen.blit(text2,(570,435))
    
def DrawThirdButton(screen):
    #pygame.draw.rect(screen, button, (1100, 300, buttonwidth2, buttonheight2))
    pygame.draw.rect(screen, button, (1100, 400, buttonwidth2, buttonheight2))
    pygame.draw.rect(screen, button, (1100, 500, buttonwidth2, buttonheight2))
    
    s_font=pygame.font.SysFont('SimHei',18)
    

    #text1=s_font.render("悔棋",True,WHITE)
    text2=s_font.render("重新开始",True,WHITE)
    text3=s_font.render("退出游戏",True,WHITE)
    
    #screen.blit(text1,(1135,315))
    screen.blit(text2,(1115,415))
    screen.blit(text3,(1115,515))

def DealClickButton1(x,y,screen):
    global choice
    if(x>=500 and x<=500 + buttonwidth):
        if(y>=100 and y<=100 + buttonheight):
            DrawSecondButton(screen)
            return True
        elif(y>=300 and y<=300 + buttonheight):
            DrawChessBoard(screen)
            DrawThirdButton(screen)
            choice = True
            return 2
        elif(y>=500 and y <= 500 + buttonheight):
            pygame.quit()
            sys.exit()
        return True
    else:
        return False
            
def DealClickButton2(x,y,screen):
    global t
    if(x>=500 and x<=500 + buttonwidth):
        if(y>=100 and y<=100 + buttonheight):
            DrawChessBoard(screen)
            return True
        elif(y>=400 and y<=400 + buttonheight):
            DrawChessBoard(screen)
            t = False
            return True
    return False

def DealClickButton3(x,y,screen):
    if(x>=1100 and x<=1100 + buttonwidth2):
        if(y>=500 and y<=500 + buttonheight2):
            pygame.quit()
            sys.exit()
            return True
        elif(y>=400 and y<=400 + buttonheight2):
            clear()
            reset()
            return True
    return False

def HighLightButton1(x,y,screen):
    if(x>=500 and x<=500 + buttonwidth):
        if(y>=100 and y<=100 + buttonheight):
            pygame.draw.rect(screen, brightbutton,(500, 100, buttonwidth, buttonheight),4)
            return True
        elif(y>=300 and y<=300 + buttonheight):
            pygame.draw.rect(screen, brightbutton,(500, 300, buttonwidth, buttonheight),4)
            return True
        elif(y>=500 and y <= 500 + buttonheight):
            pygame.draw.rect(screen, brightbutton,(500, 500, buttonwidth, buttonheight),4)
            return True
    return False

def HighLightButton2(x,y,screen):
    if(x>=500 and x<=500 + buttonwidth):
        if(y>=100 and y<=100 + buttonheight):
            pygame.draw.rect(screen, brightbutton,(500, 100, buttonwidth, buttonheight),4)
            return True
        elif(y>=400 and y<=400 + buttonheight):
            pygame.draw.rect(screen, brightbutton,(500, 400, buttonwidth, buttonheight),4)
            return True
    return False

def HighLightButton3(x,y,screen):
    if(x>=1100 and x<=1100 + buttonwidth2):
        if(y>=400 and y<=400+ buttonheight2):
            pygame.draw.rect(screen, brightbutton,(1100, 400, buttonwidth2, buttonheight2),4)
            return True
        elif(y>=500 and y<=500 + buttonheight2):
            pygame.draw.rect(screen, brightbutton,(1100, 500, buttonwidth2, buttonheight2),4)
            return True
    return False

def HeightChessBoard(x,y,screen):
    x1 = (boardwidth  - width)/2
    y1 = (boardheight - height)/2
    xl = x1 - width_between_line/2
    xr = x1 + width_between_line/2
    yt = y1 - height_between_line/2
    yb = y1 + height_between_line/2
    count = 0
    for i in range(length*length):
        if(x >= xl and x<= xr and y >=yt and y<=yb):
            pygame.draw.circle(screen, BLACK,(int(xl+ width_between_line/2), int(yt+ height_between_line/2)),5,5)
            break
        else:
            xl += width_between_line
            xr += width_between_line
            count += 1
            if(count == length):
                xl = x1 - width_between_line/2
                xr = x1 + width_between_line/2
                yt += height_between_line
                yb += height_between_line
                count = 0

def DealPutChess(x,y,screen):
    global t
    x1 = (boardwidth  - width)/2
    y1 = (boardheight - height)/2
    xl = x1 - width_between_line/2
    xr = x1 + width_between_line/2
    yt = y1 - height_between_line/2
    yb = y1 + height_between_line/2
    for i in range(length):
        for j in range(length):
            if ((xl + width_between_line*j < y < xr + width_between_line*j) and (yt + i*height_between_line < x < yb + i*height_between_line)):
                if t == True and m[i][j] == 0:
                    m[i][j] = 1
                    return True
                elif m[i][j] == 0:
                    m[i][j] = 2
                    return True
    return False
def DrawAChess(x,y,screen,color):
    x1 = (boardwidth  - width)/2
    y1 = (boardheight - height)/2
    if color==1:        
        screen.blit(blackchess,(x1 + y*width_between_line -width_between_line/4+1 ,y1 + x*height_between_line - height_between_line/4-2 ))
    if color==2:
        screen.blit(whitechess,(x1 + y*width_between_line-width_between_line/4+1,y1 + x*height_between_line - height_between_line/4-2))

def DrawChessBoardWithChess(m,screen):
    DrawChessBoard(screen)
    for i in range(length+7):
        for j in range(length+7):
            DrawAChess(i,j,screen,m[i][j])

def GetAction(a,move):
    global t
    
    i = move // length
    j = move %  length

    #print("move at ({},{})".format(i,j))
    if a[i][j] == 0 and t == True:
        a[i][j] = 1
    elif a[i][j] == 0 and t == False:
        a[i][j] = 2
    else:
        #GetAction(m,np.random.choice(100))
        return -1;

def change_turn():
    global t
    t = not(t)

def turn():
    global count
    count -= 1
    if count == 0:
        change_turn()
        count = 2
        


def JudgeSix(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            num1 = 0
            num2 = 0
            num3 = 0
            num4 = 0

            t_x, t_y = i,j
            for t in range(6):
                
                if(temp[t_x][t_y] != player):
                    break
                t_x += 1    
                num1 += 1
                
            if num1 == 6:
                count += 1
                return count

            t_x, t_y = i,j
            for t in range(6):
                
                if(temp[t_x][t_y] != player):
                    break
                t_y += 1
                num2 += 1

            if num2 == 6:
                count += 1
                return count
 
            t_x, t_y = i,j
            for t in range(6):
               
                if(temp[t_x][t_y] != player):
                    break
                t_x += 1
                t_y += 1
                num3 += 1
            if num3 == 6:
                count += 1
                return count
            
            if j >= 5 :
                t_x, t_y = i,j
                for t in range(6):
                    if(temp[t_x][t_y] != player):
                	    break
                    t_x += 1
                    t_y -= 1
                    num4 += 1
            if num4 == 6:
                count += 1
                return count
    return count




def JudgeActiveFive(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if (temp[i][j] == 0 and temp[i][j+1] == player and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == player and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == player and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0):
                count += 1
            if (j>=6 and temp[i][j] == 0 and temp[i+1][j-1] == player and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == player):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == player):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == player):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == player):
                count += 1
            if (temp[i][j] == player and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == player and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == player and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == player and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1

    return count

def JudgeSleepFive(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if(temp[i][j] == 3-player or temp[i][j+1] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j+2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_y += 1
                if(num1 == 5 and num2 == 1):
                    count += 1
            if(temp[i][j] == 3-player or temp[i+1][j] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                if(num1 == 5 and num2 == 1):
                    count += 1
            if(temp[i][j] == 3-player or temp[i+1][j+1] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j+2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 5 and num2 == 1):
                    count += 1
            if(j >= 7 and (temp[i][j] == 3-player or temp[i+1][j-1] == 3-player)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j-2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 5 and num2 == 1):
                    count += 1

            if(temp[i][j+6] == 3-player or temp[i][j+7] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_y += 1
                if(num1 == 5 and num2 == 1):
                    count += 1
            if(temp[i+6][j] == 3-player or temp[i+7][j] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                if(num1 == 5 and num2 == 1):
                    count += 1
            if(temp[i+6][j+6] == 3-player or temp[i+7][j+7] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 5 and num2 == 1):
                    count += 1
            if(j >= 7 and (temp[i+6][j-6] == 3-player or temp[i+7][j-7] == 3-player)):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 5 and num2 == 1):
                    count += 1
    #count = math.ceil(count / 2)
    return count

def JudgeActiveFour(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
    return count

def JudgeSleepFour(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if (temp[i][j] == player and temp[i][j+1] == player and temp[i][j+2] == 0 and
                temp[i][j+3] == 0 and temp[i][j+4] == player and temp[i][j+5] == player):
                count += 1
            if (temp[i][j] == player and temp[i+1][j] == player and temp[i+2][j] == 0 and
                temp[i+3][j] == 0 and temp[i+4][j] == player and temp[i+5][j] == player):
                count += 1
            if (temp[i][j] == player and temp[i+1][j+1] == player and temp[i+2][j+2] == 0 and
                temp[i+3][j+3] == 0 and temp[i+4][j+4] == player and temp[i+5][j+5] == player):
                count += 1
            if (j>=7 and temp[i][j] == player and temp[i+1][j-1] == player and temp[i+2][j-2] == 0 and
                temp[i+3][j-3] == 0 and temp[i+4][j-4] == player and temp[i+5][j-5] == player):
                count += 1

            if (temp[i][j] == player and temp[i][j+1] == 0 and temp[i][j+2] == 0 and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player):
                count += 1
            if (temp[i][j] == player and temp[i+1][j] == 0 and temp[i+2][j] == 0 and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player):
                count += 1
            if (temp[i][j] == player and temp[i+1][j+1] == 0 and temp[i+2][j+2] == 0 and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player):
                count += 1
            if (j>=7 and temp[i][j] == player and temp[i+1][j-1] == 0 and temp[i+2][j-2] == 0 and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player):
                count += 1

            if (temp[i][j] == player and temp[i][j+1] == player and temp[i][j+2] ==  player and
                temp[i][j+3] == 0 and temp[i][j+4] == 0 and temp[i][j+5] == player):
                count += 1
            if (temp[i][j] == player and temp[i+1][j] == player and temp[i+2][j] == player and
                temp[i+3][j] == 0 and temp[i+4][j] == 0 and temp[i+5][j] == player):
                count += 1
            if (temp[i][j] == player and temp[i+1][j+1] == player and temp[i+2][j+2] == player and
                temp[i+3][j+3] == 0 and temp[i+4][j+4] == 0 and temp[i+5][j+5] == player):
                count += 1
            if (j>=7 and temp[i][j] == player and temp[i+1][j-1] == player and temp[i+2][j-2] == player and
                temp[i+3][j-3] == 0 and temp[i+4][j-4] == 0 and temp[i+5][j-5] == player):
                count += 1

            if (temp[i][j] == 0 and temp[i][j+1] == player and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == 0 and temp[i][j+5] == player and
                temp[i][j+6] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == player and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == 0 and temp[i+5][j] == player and
                temp[i+6][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == player and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == 0 and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == player and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == 0 and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0):
                count += 1

            if (temp[i][j] == 0 and temp[i][j+1] == player and temp[i][j+2] == player and
                temp[i][j+3] == 0 and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == player and temp[i+2][j] == player and
                temp[i+3][j] == 0 and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == player and temp[i+2][j+2] == player and
                temp[i+3][j+3] == 0 and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == player and temp[i+2][j-2] == player and
                temp[i+3][j-3] == 0 and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0):
                count += 1

            if (temp[i][j] == 0 and temp[i][j+1] == player and temp[i][j+2] == 0 and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == player and temp[i+2][j] == 0 and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == player and temp[i+2][j+2] == 0 and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == player and temp[i+2][j-2] == 0 and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0):
                count += 1

            if ((temp[i][j] == 3-player and temp[i][j+7] == 3-player) or
                (temp[i][j] == 0 and temp[i][j+7] == 3-player) or
                (temp[i][j] == 3-player and temp[i][j+7] == 0)):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j+1
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_y += 1
                if(num1 == 4 and num2 == 2):
                    count += 1
            if ((temp[i][j] == 3-player and temp[i+7][j] == 3-player) or
                (temp[i][j] == 0 and temp[i+7][j] == 3-player) or
                (temp[i][j] == 3-player and temp[i+7][j] == 0)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+1,j
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_x += 1
                if(num1 == 4 and num2 == 2):
                    count += 1
            if ((temp[i][j] == 3-player and temp[i+7][j+7] == 3-player) or
                (temp[i][j] == 0 and temp[i+7][j+7] == 3-player) or
                (temp[i][j] == 3-player and temp[i+7][j+7] == 0)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+1,j+1
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 4 and num2 == 2):
                    count += 1
            if ((j>=7) and ((temp[i][j] == 3-player and temp[i+7][j-7] == 3-player) or
                (temp[i][j] == 0 and temp[i+7][j-7] == 3-player) or
                (temp[i][j] == 3-player and temp[i+7][j-7] == 0))):
                num1 = 0
                num2 = 0
                t_x,t_y = i+1,j-1
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 4 and num2 == 2):
                    count += 1

    return count


def JudgeActiveThree(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == 0 and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == 0 and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == 0 and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == 0 and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == 0 and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == 0 and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == 0 and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == 0 and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == 0 and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == 0 and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == 0 and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == 0 and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == 0 and
                temp[i][j+3] == player and temp[i][j+4] == player and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == 0 and
                temp[i+3][j] == player and temp[i+4][j] == player and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == 0 and
                temp[i+3][j+3] == player and temp[i+4][j+4] == player and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == 0 and
                temp[i+3][j-3] == player and temp[i+4][j-4] == player and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
    return count


def JudgeSleepThree(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if((temp[i][j] == 3-player and temp[i][j+1] == 3-player) or
               (temp[i][j] == 3-player and temp[i][j+1] == 0) or
               temp[i][j] == 0 and temp[i][j+1] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j+2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_y += 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            if((temp[i][j] == 3-player and temp[i+1][j] == 3-player) or
               (temp[i][j] == 3-player and temp[i+1][j] == 0) or
               temp[i][j] == 0 and temp[i+1][j] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            if((temp[i][j] == 3-player and temp[i+1][j+1] == 3-player) or
               (temp[i][j] == 3-player and temp[i+1][j+1] == 0) or
               temp[i][j] == 0 and temp[i+1][j+1] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j+2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            if(j >= 7 and ((temp[i][j] == 3-player and temp[i+1][j-1] == 3-player) or
               (temp[i][j] == 3-player and temp[i+1][j-1] == 0) or
               temp[i][j] == 0 and temp[i+1][j-1] == 3-player)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j-2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 3 and num2 == 3):
                    count += 1

            if((temp[i][j+6] == 3-player and temp[i][j+7] == 3-player) or
               (temp[i][j+6] == 3-player and temp[i][j+7] == 0) or
               temp[i][j+6] == 0 and temp[i][j+7] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_y += 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            if((temp[i+6][j] == 3-player and temp[i+7][j] == 3-player) or
               (temp[i+6][j] == 3-player and temp[i+7][j] == 0) or
               temp[i+6][j] == 0 and temp[i+7][j] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            if((temp[i+6][j+6] == 3-player and temp[i+7][j+7] == 3-player) or
               (temp[i+6][j+6] == 3-player and temp[i+7][j+7] == 0) or
               temp[i+6][j+6] == 0 and temp[i+7][j+7] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            if(j >= 7 and ((temp[i+6][j-6] == 3-player and temp[i+7][j-7] == 3-player) or
               (temp[i+6][j-6] == 3-player and temp[i+7][j-7] == 0) or
               temp[i+6][j-6] == 0 and temp[i+7][j-7] == 3-player)):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 3 and num2 == 3):
                    count += 1
            
    return count


def IndistinctThree(player,temp):
    pass


def JudgeActiveTwo(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == player and temp[i][j+4] == 0 and temp[i][j+5] == 0 and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == player and temp[i+4][j] == 0 and temp[i+5][j] == 0 and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == player and temp[i+4][j+4] == 0 and temp[i+5][j+5] == 0 and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == player and temp[i+4][j-4] == 0 and temp[i+5][j-5] == 0 and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
            
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == 0 and temp[i][j+4] == player and temp[i][j+5] == 0 and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == 0 and temp[i+4][j] == player and temp[i+5][j] == 0 and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == 0 and temp[i+4][j+4] == player and temp[i+5][j+5] == 0 and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == 0 and temp[i+4][j-4] == player and temp[i+5][j-5] == 0 and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
            
            if (temp[i][j] == 0 and temp[i][j+1] == 0 and temp[i][j+2] == player and
                temp[i][j+3] == 0 and temp[i][j+4] == 0 and temp[i][j+5] == player and
                temp[i][j+6] == 0 and temp[i][j+7] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j] == 0 and temp[i+2][j] == player and
                temp[i+3][j] == 0 and temp[i+4][j] == 0 and temp[i+5][j] == player and
                temp[i+6][j] == 0 and temp[i+7][j] == 0):
                count += 1
            if (temp[i][j] == 0 and temp[i+1][j+1] == 0 and temp[i+2][j+2] == player and
                temp[i+3][j+3] == 0 and temp[i+4][j+4] == 0 and temp[i+5][j+5] == player and
                temp[i+6][j+6] == 0 and temp[i+7][j+7] == 0):
                count += 1
            if (j>=7 and temp[i][j] == 0 and temp[i+1][j-1] == 0 and temp[i+2][j-2] == player and
                temp[i+3][j-3] == 0 and temp[i+4][j-4] == 0 and temp[i+5][j-5] == player and
                temp[i+6][j-6] == 0 and temp[i+7][j-7] == 0):
                count += 1
    return count


def JudgeSleepTwo(player,temp):
    count = 0
    for i in range(length):
        for j in range(length):
            if((temp[i][j] == 3-player and temp[i][j+1] == 3-player) or
               (temp[i][j] == 3-player and temp[i][j+1] == 0) or
               temp[i][j] == 0 and temp[i][j+1] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j+2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_y += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if((temp[i][j] == 3-player and temp[i+1][j] == 3-player) or
               (temp[i][j] == 3-player and temp[i+1][j] == 0) or
               temp[i][j] == 0 and temp[i+1][j] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if((temp[i][j] == 3-player and temp[i+1][j+1] == 3-player) or
               (temp[i][j] == 3-player and temp[i+1][j+1] == 0) or
               temp[i][j] == 0 and temp[i+1][j+1] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j+2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if(j >= 7 and ((temp[i][j] == 3-player and temp[i+1][j-1] == 3-player) or
               (temp[i][j] == 3-player and temp[i+1][j-1] == 0) or
               temp[i][j] == 0 and temp[i+1][j-1] == 3-player)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+2,j-2
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 2 and num2 == 4):
                    count += 1

            if((temp[i][j+6] == 3-player and temp[i][j+7] == 3-player) or
               (temp[i][j+6] == 3-player and temp[i][j+7] == 0) or
               temp[i][j+6] == 0 and temp[i][j+7] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_y += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if((temp[i+6][j] == 3-player and temp[i+7][j] == 3-player) or
               (temp[i+6][j] == 3-player and temp[i+7][j] == 0) or
               temp[i+6][j] == 0 and temp[i+7][j] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if((temp[i+6][j+6] == 3-player and temp[i+7][j+7] == 3-player) or
               (temp[i+6][j+6] == 3-player and temp[i+7][j+7] == 0) or
               temp[i+6][j+6] == 0 and temp[i+7][j+7] == 3-player):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if(j >= 7 and ((temp[i+6][j-6] == 3-player and temp[i+7][j-7] == 3-player) or
               (temp[i+6][j-6] == 3-player and temp[i+7][j-7] == 0) or
               temp[i+6][j-6] == 0 and temp[i+7][j-7] == 3-player)):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j
                for t in range(6):
                    if temp[t_x][t_y] == player:
                        num1 += 1
                    elif temp[t_x][t_y] == 0:
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if ((temp[i][j] == 3-player and temp[i][j+7] == 3-player) or
                (temp[i][j] == 0 and temp[i][j+7] == 3-player) or
                (temp[i][j] == 3-player and temp[i][j+7] == 0)):
                num1 = 0
                num2 = 0
                t_x,t_y = i,j+1
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_y += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if ((temp[i][j] == 3-player and temp[i+7][j] == 3-player) or
                (temp[i][j] == 0 and temp[i+7][j] == 3-player) or
                (temp[i][j] == 3-player and temp[i+7][j] == 0)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+1,j
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_x += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if ((temp[i][j] == 3-player and temp[i+7][j+7] == 3-player) or
                (temp[i][j] == 0 and temp[i+7][j+7] == 3-player) or
                (temp[i][j] == 3-player and temp[i+7][j+7] == 0)):
                num1 = 0
                num2 = 0
                t_x,t_y = i+1,j+1
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_x += 1
                    t_y += 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            if ((j>=7) and ((temp[i][j] == 3-player and temp[i+7][j-7] == 3-player) or
                (temp[i][j] == 0 and temp[i+7][j-7] == 3-player) or
                (temp[i][j] == 3-player and temp[i+7][j-7] == 0))):
                num1 = 0
                num2 = 0
                t_x,t_y = i+1,j-1
                for t in range(6):
                    if(temp[t_x][t_y] == player):
                        num1 += 1
                    elif(temp[t_x][t_y] == 0):
                        num2 += 1
                    t_x += 1
                    t_y -= 1
                if(num1 == 2 and num2 == 4):
                    count += 1
            
    return count

def transfer(a,b):
    return a*length + b

def distransfer(move,length):
    i = move // length
    j = move %  length
    return i, j

def SearchArea():
    for i in range(length):
        for j in range(length):
            if m[i][j] != 0:
                searchset.add(transfer(i-1,j))
                searchset.add(transfer(i+1,j))
                searchset.add(transfer(i,j-1))
                searchset.add(transfer(i,j+1))
                searchset.add(transfer(i-1,j-1))
                searchset.add(transfer(i-1,j+1))
                searchset.add(transfer(i+1,j-1))
                searchset.add(transfer(i+1,j+1))
                searchset.add(transfer(i-2,j))
                searchset.add(transfer(i-2,j-2))
                searchset.add(transfer(i-2,j+2))
                searchset.add(transfer(i+2,j))
                searchset.add(transfer(i+2,j+1))
                searchset.add(transfer(i+2,j+2))
                searchset.add(transfer(i,j-2))
                searchset.add(transfer(i,j+2))


def TSS(player,temp):
    '''Judge if there is any threats to win the game.
       ActiveFive, ActiveFour, SleepFive, Sleep is needed to be searched.'''
    a = JudgeActiveFive(player,temp)
    b = JudgeActiveFour(player,temp)
    c = JudgeSleepFive(player,temp)
    d = JudgeSleepFour(player,temp)
    '''
    if a:
        print("find {} Active 5!!".format(a))
    if b:
        print("find {} Active 4!!".format(b))
    if c:
        print("find {} Sleep 5!!".format(c))
    if d:
        print("find {} Sleep 4!!".format(d))
    '''
    if(a + b + c + d == 0):
        #print("no threattttttttttttttttttt")
        return 0
    elif(2*a + 2*b + c + d == 1):
        #print("1 threat~~~~~~~~~~~~~~~~~~~~~~~")
        return 1
    elif(2*a + 2*b + c + d == 2):
        #print("2 threat----------------------")
        return 2
    elif(2*a + 2*b + c + d >= 3):
        #print("more than 3 threat...........")
        return 3
    	
def CTW(player):
    '''Judge if there is chance to win directly'''
    global m
    a = JudgeActiveFive(player,m)
    b = JudgeActiveFour(player,m)
    c = JudgeSleepFive(player,m)
    d = JudgeSleepFour(player,m)
    '''
    if a:
        print("you have {} Active 5!!".format(a))
    if b:
        print("you have {} Active 4!!".format(b))
    if c:
        print("you have {} Sleep 5!!".format(c))
    if d:
        print("you have {} Sleep 4!!".format(d))
    '''
    if (count == 2):
        if(b or d):
            return True
    elif (count == 1):
        if(a or c):
            return True
    return False

def find_win_move(player, temp):
    for move in temp:
        w = copy.deepcopy(m)
        if GetAction(w,move) == -1:
            continue
        if count == 1:
            if(JudgeSix(player,w)):
                return move
        elif count == 2:
            if(JudgeSleepFive(player,w) or JudgeActiveFive(player,w)):
                return move

    return -1


def game_end(a):
    if(JudgeSix(1,a)):
        return True, 1
    elif(JudgeSix(2,a)):
        return True, 2
    else:
        return False,-1


def evaluate(player,move):
    temp = copy.deepcopy(m)

    if(GetAction(temp,move) == -1):
        return -1
    score = 0

    if(TSS(player[1], temp) >= 2):
        return -1000000

    if(TSS(player[1], temp) == 1 and count == 1):
        return -1000000

    n = JudgeActiveFive(player[1], temp)
    if(n):
        score -= 50000 * n
        #print("you have {} A5".format(n))
    if score < 0:
        return -100000

    n = JudgeActiveFour(player[1], temp)
    if(n):
        score -= 50000 * n
        #print("you have {} A4".format(n))
    if score < 0:
        return -100000

    n = JudgeSleepFive(player[1], temp)    
    if(n):
        score -= 50000 * n
        #print("you have {} S5".format(n))

    n = JudgeSleepFour(player[1], temp)
    if(n):
        score -= 40000 * n
        #print("you have {} S4".format(n))
        

    n = JudgeActiveThree(player[1], temp)
    if(n):
        score -= 8000 * n
        #print("you have {} A3".format(n))

    n = JudgeSleepThree(player[1], temp)
    if(n):
        score -= 5000 * n
        #print("you has {} S3".format(n))

    n = JudgeActiveTwo(player[1], temp)
    if(n):
        score -= 2000 * n
        #print("you have {} A2".format(n))
    
    n = JudgeSix(player[0], temp)
    if(n):
        score += 100000 * n
        #print("B6 has 6")
        return score

    b = JudgeActiveFour(player[0], temp)
    if(b):
        score += 15000 * b
        #print("B6 has {} A4".format(b))
    if(b >=2 ):
        score += 100000
        #print("B6 has double A4")

    c = JudgeSleepFour(player[0], temp)
    if(c):
        score += 6000 * c
        #print("B6 has {} S4".format(c))

    if(b and c):
        score += 100000
        #print("B6 has (S5 and A4) or (S4 and A4)")
        return score

    n = JudgeActiveThree(player[0], temp)
    if(n):
        score += 4000 * n
        #print("B6 has {} A3".format(n))

    n = JudgeSleepThree(player[0], temp)
    if(n):
        score += 2000 * n
        #print("B6 has {} S3".format(n))

    n = JudgeActiveTwo(player[0], temp)
    if(n):
        score += 2000 * n
        #print("B6 has {} A2".format(n))

    return score    


def agent_to_play():
    #GetAction(m,np.random.choice(100))
    global firstmove
    maxmove = 350
    maxscore = -100000
    player = [2,1]
    SearchArea()
    #print(a,b)
    if CTW(player[0]):
        #print("chance to win")
        maxscore = 100000
        maxmove = find_win_move(player[0], searchset)
        if maxmove == -1:
            for move in searchset:
                if firstmove == True:
                    if m[18][18] == 0:
                        maxmove = 684
                    elif m[18][17] == 0:
                        maxmove = 683
                    firstmove = False
                    break
                else:
                    tempscore = evaluate(player,move)
                    if(tempscore == -1):
                        continue
                    if tempscore > maxscore:
                        maxscore = tempscore
                        maxmove = move
            
    else:
        if firstmove == True:
            if m[18][18] == 0:
                maxmove = 684
            elif m[18][17] == 0:
                maxmove = 683
            firstmove = False
        else: 
            for move in searchset:
                tempscore = evaluate(player,move)
                if(tempscore == -1):
                    continue
                if tempscore > maxscore:
                    maxscore = tempscore
                    maxmove = move
    searchset.clear()
    #print("maxscore = {}".format(maxscore))
    #print("maxmove = {}".format(maxmove))
    GetAction(m, maxmove)
    #print("end move")
    

def start_game():
    pygame.init()

    FPS = 30
    fpsClock = pygame.time.Clock()

    #set up the window
    screen = pygame.display.set_mode((boardwidth, boardheight))

    pygame.display.set_caption('BetaConnectSix')

    mousex = 0
    mousey = 0

    screen.fill(WHITE)
    s_font=pygame.font.SysFont('SimHei',30)

    while True:
        global started,choice,iscover, length, blackchess, whitechess,t,m,end,firstmove,searchset
        
        mouseClicked = False
        
        screen.fill(WHITE)
        
        if started == False:
            DrawFirstButton(screen)
            if iscover == 1:
                HighLightButton1(mouse[0],mouse[1],screen)
        elif choice == False:
            DrawSecondButton(screen)
            if iscover == 1:
                HighLightButton2(mouse[0],mouse[1],screen)
        elif end == False:
            DrawChessBoardWithChess(m,screen)
            DrawThirdButton(screen)
            HeightChessBoard(mouse[0],mouse[1],screen)
            if iscover == 1:
                HighLightButton3(mouse[0],mouse[1],screen)
            isend,winner = game_end(m)
            if(isend):
                if(winner == 1):
                    text1=s_font.render("Human win!",True,BLACK)
                    end = True
                else:
                    text1=s_font.render("BetaSix win!",True,BLACK)
                    end = True
                screen.blit(text1,(1100,150))
            elif(t == False):
                agent_to_play()
                turn()
        else:
            DrawChessBoardWithChess(m,screen)
            DrawThirdButton(screen)
            if iscover == 1:
                HighLightButton3(mouse[0],mouse[1],screen)

            if(winner == 1):
                text1=s_font.render("Human win!",True,BLACK)
            else:
                text1=s_font.render("BetaSix win!",True,BLACK)
            screen.blit(text1,(1100,150))
        
        for event in pygame.event.get():
            
            mouse = pygame.mouse.get_pos()
            if(started == False):
                if(HighLightButton1(mouse[0],mouse[1],screen)):
                    iscover = 1
                else:
                    iscover = 0
            elif(choice == False):
                if(HighLightButton2(mouse[0],mouse[1],screen)):
                    iscover = 1
                else:
                    iscover = 0
            else:
                if(HighLightButton3(mouse[0],mouse[1],screen)):
                    iscover = 1
                else:
                    iscover = 1
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                mousex, mousey = event.pos
                mouseClicked = True
                if(started == False):
                    if(DealClickButton1(mousex,mousey,screen)):
                        started = True

                elif(choice == False):
                    if(DealClickButton2(mousex,mousey,screen)):
                        choice = True


                elif end == False:
                    DealClickButton3(mousex,mousey,screen)
                    if(t == True and DealPutChess(mousey,mousex,screen)):
                        turn()
                    
                else:
                    DealClickButton3(mousex,mousey,screen)
                


        pygame.display.update()
        fpsClock.tick(FPS)
            

'''if __name__ == '__main__':
    main()'''

start_game()
